package Units;
import java.util.ArrayList;
import main.*;
/**
 * checks which units can be built
 * @author Justin Yu
 *
 */
public class ValidUnits {
	Player p;
	ArrayList<String> valid = new ArrayList<String>();
	/**
	 * base constructor
	 * @param p1 owner
	 */
	public ValidUnits(Player p1) {
		p = p1;
		checkUnits();
	}
/**
 * ehcks each unit type
 */
	public void checkUnits() {
		valid = new ArrayList<String>();
		if(affordable(Cavalry.getCost(), p.getRes().getArray()) && checkTech(Cavalry.tech))
			valid.add("Cavalry");
		if(affordable(FootSoldier.getCost(), p.getRes().getArray()))
			valid.add("Foot Soldier");
		if(affordable(Scout.getCost(), p.getRes().getArray()))
			valid.add("Scout");
		if(affordable(Tank.getCost(), p.getRes().getArray()) && checkTech(Tank.tech))
			valid.add("Tank");
	}
	/**
	 * does a string check for tech name
	 * @param reqTech the tech needed
	 * @return have it?
	 */
	public boolean checkTech(String[] reqTech) {
		if(reqTech == null)
			return true;

		for(int i = 0; i < reqTech.length; i++) {
			if(!(p.getRes().getTree().checkIfComplete(reqTech[i])))
				return false;
		}
		return true;
	}
/**
 * can you buy the unit type?
 * @param cost
 * @param res
 * @return
 */
	public boolean affordable(int[] cost, int[] res) {
		try {
			for(int i=0; i<res.length; i++) {
				if(cost[i] > res[i])
					return false;
			}
		} catch(IndexOutOfBoundsException e) {
			System.err.println("INDICIES ARE OUT OF BOUNDS");
			return false;
		}

		return true;
	}

	public ArrayList<String> getValid() {
		return valid;
	}

}
